package sample.plugin.html.lang;

import android.content.res.AssetManager;

import com.rapid.api.ext.plugin.AndroidPlugins;
import com.rapid.api.framework.domain.editor.EditorThemeService;
import com.rapid.api.lang.Language;
import com.rapid.api.lang.factory.TextMateLanguageFactory;

import org.eclipse.tm4e.core.registry.IGrammarSource;
import org.eclipse.tm4e.core.registry.IThemeSource;

import java.io.IOException;
import java.io.Reader;

/**
 * Html语言工厂类
 */
public class HtmlLanguageFactory extends TextMateLanguageFactory {
    private EditorThemeService editorThemeService;

    /**
     * 这里使用构造依赖注入，QiStudio会自动传入对应的对象
     */
    public HtmlLanguageFactory(EditorThemeService editorThemeService) {
        this.editorThemeService = editorThemeService;
    }

    @Override
    public String getLanguageId() {
        return HtmlLanguage.ID;
    }

    @Override
    public String getAssociateFileType() {
        return "html";
    }

    @Override
    public Language newLanguage() {
        try {
            AssetManager assets = AndroidPlugins.getContext(this).getAssets();
            IGrammarSource source = readGrammarSource(
                    assets.open("textmate/html.plist"),
                    "textmate/html.plist"
            );
            Reader reader = readConfiguration(assets.open("textmate/html-settings.json"));
            IThemeSource themeSource = getThemeSource();
            HtmlLanguage language = new HtmlLanguage(source, reader, themeSource);
            return language;
        } catch (IOException e) {
            e.printStackTrace();
        }
        return null;
    }
}
